package com.gofbox.consume;

import com.alibaba.dubbo.config.annotation.Reference;
import com.box.user.model.BaseUser;
import com.gofbox.api.user.IBaseUserRemote;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by aoc on 2016/10/26.
 */
@RestController
public class BaseUserController {
    protected static final Logger logger = LoggerFactory.getLogger(BaseUserController.class);
    @Reference(version = "1.0.0")
    private IBaseUserRemote baseUserRemote;

    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    public String getUser(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "userId", required = true) long userId) {
        try {
            if (userId <= 0)
                return "error";
            List<BaseUser> baseUsers = baseUserRemote.getUser(Arrays.asList(userId), 0, 1);
        } catch (Exception e) {
            logger.error("getUser  error userId:" + userId);
        }
        return "success";

    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public String addUser(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "userName", required = true) String userName,
            @RequestParam(value = "passwd", required = true) String passwd, @RequestParam(value = "email", required = true) String email) {
        BaseUser baseUser = new BaseUser();
        baseUser.setUserName(userName);
        baseUser.setPassword(passwd);
        baseUser.setEmail(email);
        baseUser.setCreateTime(new Date());
        boolean flag = false;
        try {
            flag = baseUserRemote.addUser(baseUser);
        } catch (Exception e) {
            logger.error("addUser  error userId:" + userName);
        }
        return flag == true ? "success" : "fail";
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public String updateUser(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "userId", required = true) long userId,
            @RequestParam(value = "userName", required = true) String userName, @RequestParam(value = "passwd", required = true) String passwd,
            @RequestParam(value = "email", required = true) String email) {
        BaseUser baseUser = new BaseUser();
        baseUser.setId(userId);
        baseUser.setUserName(userName);
        baseUser.setPassword(passwd);
        baseUser.setEmail(email);
        baseUser.setUpdateTime(new Date());
        boolean flag = false;
        try {
            flag = baseUserRemote.addUser(baseUser);
        } catch (Exception e) {
            logger.error("updateUser  error userId:" + userName);
        }
        return flag == true ? "success" : "fail";
    }

    @RequestMapping(value = "/delUser", method = RequestMethod.POST)
    public String delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "userId", required = true) long userId) {
        BaseUser baseUser = new BaseUser();
        baseUser.setId(userId);
        boolean flag = false;
        try {
            flag = baseUserRemote.delUser(baseUser);
        } catch (Exception e) {
            logger.error("delUser  error userId:" + userId);
        }
        return flag == true ? "success" : "fail";
    }

}
